<template>
  <div class="app-container">
    <el-card>
      <el-form ref="form" :model="form" label-width="80px" :disabled="readFlag">
        <el-row>
          <el-col :span="24">
            <el-form-item label="订单编号" prop="id">
              {{ form.id }}
            </el-form-item></el-col
          >
          <el-col :span="12">
            <el-form-item label="商品数量" prop="quantity">
              {{ form.quantity }}
            </el-form-item></el-col
          >
          <el-col :span="12">
            <el-form-item label="折扣" prop="discount">
              <dict-tag
                :options="dict.type.sale_discount"
                :value="form.discount"
              /> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="合计" prop="amount">
              {{ form.amount }}
            </el-form-item></el-col
          >
          <el-col :span="12">
            <el-form-item label="应收" prop="receivable">
              {{ form.receivable }}
            </el-form-item></el-col
          >
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              {{ form.remark }}
            </el-form-item></el-col
          >
          <el-col :span="24">
            <el-form-item label="状态" prop="status">
              <dict-tag
                :options="dict.type.order_status"
                :value="form.status"
              /> </el-form-item
          ></el-col>
        </el-row>
        <el-divider content-position="center">订单详细信息</el-divider>
        <el-table
          :data="saleDetailList"
          :row-class-name="rowSaleDetailIndex"
          ref="saleDetail"
        >
          <el-table-column label="编号" prop="skuId"> </el-table-column>
          <el-table-column label="商品" prop="skuName">
            <template slot-scope="scope">
              <image-preview :src="scope.row.imgUrl" :width="50" :height="50" />
              {{ scope.row.skuName }}
            </template>
          </el-table-column>
          <el-table-column label="颜色" prop="color">
            <template slot-scope="scope">
              <dict-tag
                :options="dict.type.sku_color"
                :value="scope.row.color"
              />
            </template>
          </el-table-column>
          <el-table-column label="尺码" prop="size">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sku_size" :value="scope.row.size" />
            </template>
          </el-table-column>
          <el-table-column label="价格" prop="skuPrice"> </el-table-column>
          <el-table-column label="数量" prop="quantity"> </el-table-column>
          <el-table-column label="小计" prop="detailPrice"> </el-table-column>
        </el-table>
      </el-form>
      <div class="footer">
        <el-button type="success" @click="submitForm('2')">支 付</el-button>
        <el-button type="warning" @click="submitForm('9')">作 废</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import { getInfo, updateInfo } from "@/api/sale/info";
import { createOrder } from "@/api/sell/payment";

export default {
  name: "Pending",
  dicts: ["sale_discount", "order_status", "sku_color", "sku_size"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 订单详细表格数据
      saleDetailList: [],
      // 表单参数
      form: {},
      readFlag: false,
    };
  },
  created() {
    const id = this.$route.params.id;
    if (id) {
      this.getById(id);
    }
  },
  methods: {
    // 取消按钮
    cancel() {
      this.$tab.closePage();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        quantity: null,
        discount: null,
        amount: null,
        receivable: null,
        remark: null,
        status: null,
        createTime: null,
        updateTime: null,
      };
      this.saleDetailList = [];
      this.resetForm("form");
    },
    /** 修改按钮操作 */
    getById(id, readFlag = false) {
      this.reset();
      this.readFlag = readFlag;
      getInfo(id).then((response) => {
        this.form = response.data;
        this.saleDetailList = response.data.saleDetailList;
      });
    },
    /** 提交按钮 */
    submitForm(status) {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          const param = Object.assign({}, this.form);
          param.saleDetailList = this.saleDetailList;
          param.status = status;
          // 支付
          if (param.status == "2") {
            this.$modal.msgSuccess("操作成功");
            createOrder(param.id).then((response) => {
              window.open(process.env.VUE_APP_BASE_API + response.msg, "_blank");
              this.cancel();
            });
          } else if (param.status == "9") {
            // 作废
            updateInfo(param).then((response) => {
              this.$modal.msgSuccess("操作成功");
              this.cancel();
            });
          }
        }
      });
    },
    /** 订单详细序号 */
    rowSaleDetailIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
  },
};
</script>
<style scoped>
.footer {
  text-align: center;
  justify-items: center;
  margin-top: 20px;
}
</style>
